apiready = function(){
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../pop"], function(V, $api, $, $app, service, $pop) {
	var vm = {
		el: '.container',
		data: {
			endTime: '',
			startTime: '',
			remindTime: '',
			list: [],
			timeMs: 9,
			tag: 1,
			page: 1,
			is_future: 0,
			activeIndex: 0,
			timeList: '',
			timeTxt: '',
			hourMinute: '',
			have_data: true,
			timer: ''
		},
		methods: {
			init: function(isNull){
				if(this.have_data){
					this.getList(isNull);
				}else{
					$app.toast("没有更多数据了！");
				}
			},
			getList: function(isNull){
				var self = this;
				var _isNull = typeof isNull == 'boolean' ? isNull : false;
				$app.ajax(service.indexTagGoods,{
		  			data: {
						values: {
							tag: self.tag,
							page: self.page,
							is_future: self.is_future
						}
					}
		  		},function(res){
		  			console.log("shop_discount："+JSON.stringify(res));
		  			if(res.code == 0){
		  				if(_isNull){
		  					self.list = [];
		  				}
		  				if(Array.isArray(res.data.list)){
		  					if(res.data.list.length > 0){
		  						for(var i=0; i < res.data.list.length; i++){
		  							self.list.push(res.data.list[i]);
		  						}
		  					}
		  				}
	  					self.initTimer();
		  				self.have_data = res.data.list.length > 0 ? true : false;
		  				self.page++;
		  			}
		  		});
			},
			initTimer: function() {
				var self = this;
				var oDate = new Date();
				var hDate = new Date();
				var dDate = new Date();
				hDate.setHours(hDate.getHours() + 1);
				dDate.setDate(dDate.getDate() + 1);
				var now_hh = oDate.getHours();

				var h_year = hDate.getFullYear();
				var h_mouth = hDate.getMonth()+1 > 9 ? hDate.getMonth()+1 : '0'+(hDate.getMonth()+1);
				var h_day = hDate.getDate() > 9 ? hDate.getDate() : '0'+hDate.getDate();
				var h_hours = hDate.getHours() > 9 ? hDate.getHours() : '0'+hDate.getHours();

				var d_year = dDate.getFullYear();
				var d_mouth = dDate.getMonth()+1 > 9 ? dDate.getMonth()+1 : '0'+(dDate.getMonth()+1);
				var d_day = dDate.getDate() > 9 ? dDate.getDate() : '0'+dDate.getDate();

				var countTime = 0;
				if(this.is_future == 2){
					self.timeTxt = '明日00:00即将开抢';
					self.hourMinute = "00:00";
					countTime = (new Date(d_year+'/'+d_mouth+'/'+d_day+" 00:00")).getTime();
					console.log(d_year+'/'+d_mouth+'/'+d_day+" 00:00");
					console.log();
				}else if(this.is_future == 0){
					self.hourMinute = (now_hh > 9 ? now_hh : '0'+now_hh)+":00";
					self.timeTxt = (now_hh > 9 ? now_hh : '0'+now_hh)+':00正在疯抢';
					countTime =(new Date(h_year+'/'+h_mouth+'/'+h_day+" "+h_hours+":00")).getTime();
				}else{
					self.hourMinute = (hDate.getHours() > 9 ? hDate.getHours() : '0'+hDate.getHours())+":00";
					self.timeTxt = (hDate.getHours() > 9 ? hDate.getHours() : '0'+hDate.getHours())+':00即将开始';
					countTime =(new Date(h_year+'/'+h_mouth+'/'+h_day+" "+h_hours+":00")).getTime();
				}
				clearInterval(self.timer);
				self.timer = setInterval(function() {
					var sumTime = countTime - (new Date()).getTime();
					var h = 0,
						s = 0,
						m = 0,
						ms = 9;
					if(sumTime > 0) {
						h = Math.floor(sumTime / (1000 * 60 * 60));
						s = Math.floor(sumTime % (1000 * 60 * 60) / (1000 * 60));
						m = Math.floor((sumTime % (1000 * 60 * 60) % (1000 * 60)) / 1000);
					}
					h = h > 9 ? h.toString() : "0" + h;
					s = s > 9 ? s.toString() : "0" + s;
					m = m > 9 ? m.toString() : "0" + m;
					var time = h + s + m;
					if(time.length > 6) {
						time = time.substr(0, 1);
					}
					self.timeList = time.split("");
					var msTime = setInterval(function() {
						self.timeMs = ms;
						ms--;
						if(ms < 0) {
							ms = 9;
						}
					}, 100);
					if(time == '000000') {
						clearInterval(self.timer);
						clearInterval(msTime);
						self.timeMs = 0;
						self.page = 1;
						self.is_futrure = 0;
						self.have_data = true;
						self.init();
					}
				}, 1000);
			},
			changeNav: function(et) {
				var index = +et.currentTarget.dataset.index;
				this.activeIndex = index;
				this.is_future = index;
				this.page = 1;
				this.have_data = true;
				this.init(true);
			},
			//--立刻购买
			atOnceBuy: function(et){
				et.stopPropagation();
				var goodsId = et.currentTarget.dataset.goodsid;
				var is_group = +et.currentTarget.dataset.group == 1 ? true : false;
	  			$app.goGoodsDetail(goodsId,is_group);
			},
			//--提醒我
			remindMeBuy: function(et){
				et.stopPropagation();
				var oDate = new Date();
				var hDate = new Date();
				oDate.setHours(hDate.getHours() + 1);
				var hours = oDate.getHours() > 9 ? oDate.getHours() : '0'+oDate.getHours();

				var h_year = hDate.getFullYear();
				var h_mouth = hDate.getMonth()+1 > 9 ? hDate.getMonth()+1 : '0'+(hDate.getMonth()+1);
				var h_day = hDate.getDate() > 9 ? hDate.getDate() : '0'+hDate.getDate();
				var h_full = h_year+"-"+h_mouth+"_"+h_day+" "+hours+":57";

				var o_year = oDate.getFullYear();
				var o_mouth = oDate.getMonth()+1 > 9 ? oDate.getMonth()+1 : '0'+(oDate.getMonth()+1);
				var o_day = oDate.getDate() > 9 ? oDate.getDate() : '0'+oDate.getDate();
				var o_full = o_year+'-'+o_mouth+"-"+o_day+" 23:57";

				if(this.is_futrure == 1){
					remind_time = h_full;
				}else{
					remind_time = o_full;
				}
				var unionid = $api.getStorage("shop_unionid"), self = this;
				var goods_id = et.currentTarget.dataset.goodsid;
				$app.ajax(service.indexGoodsRemind,{
		  			data: {
						values: {
							unionid: unionid,
							goods_id: goods_id,
							remind_type: self.is_future,
							remind_time: remind_time
						}
					}
		  		},function(res){
		  			//console.log("shop_discount："+JSON.stringify(res));
		  			if(res.code == 0){
		  				$pop.tip('设置成功，在开抢前3分钟提醒！');
		  			}
		  		});
			}
		},
		mounted: function() {
			var self = this;
			//--加载更多
			api.addEventListener({
				name: 'scrolltobottom',
				extra:{
					threshold: 0            //设置距离底部多少距离时触发，默认值为0，数字类型
				}
			}, function(ret, err){
				self.init(false);
			});
		},
		created: function(){
			this.init();
		}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
